<template>
  <d2-container>
    <el-form :inline="true" :model="dataSearch" ref="dataSearch">
      <el-form-item label="员工姓名" prop="name">
        <el-input v-model="dataSearch.name" placeholder="请输入员工姓名"></el-input>
      </el-form-item>

      <el-form-item >
                <el-date-picker
                        v-model="dataSearch.start_time"
                        type="date"
                        placeholder="开始日期"
                        style='width: 150px'
                        >
                </el-date-picker>
            </el-form-item>

            <el-form-item >
                <el-date-picker
                        v-model="dataSearch.end_time"
                        type="date"
                        placeholder="结束日期"
                        style='width:150px'
                        >
                </el-date-picker>
            </el-form-item>

      <el-form-item>
        <el-button type="primary" @click="formSubmit">
          <d2-icon name="search" />查询
        </el-button>
      </el-form-item>
      <el-form-item>
        <el-button @click="formReset">
          <d2-icon name="refresh" />重置
        </el-button>
      </el-form-item>
      <!-- <a href="#/work-record-new">
        <el-button type="primary" style="float:right" icon="el-icon-plus">补打卡</el-button>
      </a> -->
    </el-form>
    <el-table :data="dataList" border style="width: 100%">
      <el-table-column prop="staff" label="员工姓名"></el-table-column>
      <el-table-column prop="recordType" label="打卡状态"></el-table-column>
      <el-table-column prop="createdAt" label="打卡时间"></el-table-column>
      <el-table-column prop="recordWay" label="打卡地址"></el-table-column>
      <el-table-column prop="memo" label="打卡备注"></el-table-column>
      <el-table-column prop="address" label="地址"></el-table-column>
      <el-table-column fixed="right" label="操作" v-if="!teacher">
        <template slot-scope="scope">
          <el-button @click="goWork(scope.row)" type="text" size="small" v-if="scope.row.recordType == '下班'">上班</el-button>
          <el-button @click="offWork(scope.row)" type="text" size="small" v-if="scope.row.recordType == '上班'">下班</el-button>
          <el-button @click="deleteClick(scope.row)" type="text" size="small">删除</el-button>
        </template>
      </el-table-column>
    </el-table>
    <el-pagination
      background
      layout="total,sizes,prev, pager, next,jumper"
      :page-size="pageSize"
      :current-page="currentPage"
      :total="total"
      @current-change="handleCurrentChange"
      @size-change="handlerSizeChange"
      @next-click="handleNextPage"
      @prev-click="handlePrevClick"
    ></el-pagination>
  </d2-container>
</template>

<script>
import request from "@/plugin/axios";
import { mapState } from "vuex";
import formatTime from "@/libs/util.formatTime"

export default {
  data() {
    return {
      dataList: [],
      total: 0,
      pageSize: 10,
      currentPage: 1,
      classroom: [],
      dataSearch: {},
      school : undefined,
      dataSearch:{
        name : undefined,
        start_time : undefined,
        end_time : undefined
      },
      teacher : false
      
    };
  },
    computed:{
    ...mapState('d2admin/user',['info'])
  },
  methods: {
    formSubmit() {
      this.dataSearch.page = this.currentPage;
      this.dataSearch.pageSize = this.pageSize;
      this.getDataList(this.dataSearch);
    },
    formReset() {
      this.$refs.dataSearch.resetFields();
      this.dataSearch = {
        name : undefined,
        start_time : undefined,
        end_time : undefined
      }
    },
    handleClick(e) {
      this.$router.push({ path: "/classroom-detail?id=" + e._id });
    },

    handleCurrentChange(currentPage) {
      this.currentPage = currentPage;
      this.dataSearch.page = this.currentPage;
      this.dataSearch.pageSize = this.pageSize;
      this.getDataList(this.dataSearch);
    },
    handlerSizeChange(pageSize) {
      this.pageSize = pageSize;
      this.dataSearch.page = this.currentPage;
      this.dataSearch.pageSize = this.pageSize;
      this.getDataList(this.dataSearch);
    },
    handleNextPage() {
      this.currentPage = this.currentPage + 1;
      this.dataSearch.page = this.currentPage;
      this.dataSearch.pageSize = this.pageSize;
      this.getDataList(this.dataSearch);
    },
    handlePrevClick() {
      this.currentPage = this.currentPage - 1;
      this.dataSearch.page = this.currentPage;
      this.dataSearch.pageSize = this.pageSize;
      this.getDataList(this.dataSearch);
    },
    deleteClick(e) {
      this.$confirm("此操作将删除该打卡记录, 是否继续?", "提示", {
        confirmButtonText: "确定",
        cancelButtonText: "取消",
        type: "warning"
      })
        .then(() => {
          request({
            url: "/workRecord/deleteWorkRecord",
            method: "post",
            data: { id: e._id }
          }).then(res => {
            if (res.code == 200) {
              this.$message({
                message: "删除成功"
              });
              this.getDataList(this.dataSearch);
            }
          });
        })
        .catch(() => {
          this.$message({
            type: "info",
            message: "已取消删除"
          });
        });
    },

    goWork(data){
      let datax = {}
      datax.id = data._id
      datax.recordType = 0
      request({
        url: "/workRecord/workRecordStatusChange",
        method: "post",
        data: datax
      }).then(res => {
          if (res.code == 200){
            this.dataSearch.page = this.currentPage;
      this.dataSearch.pageSize = this.pageSize;
            this.getDataList(this.dataSearch)
          }
      });
    },

    offWork(data){
      let datax = {}
      datax.id = data._id
      datax.recordType = 1
      request({
        url: "/workRecord/workRecordStatusChange",
        method: "post",
        data: datax
      }).then(res => {
        if (res.code == 200){
          this.dataSearch.page = this.currentPage;
      this.dataSearch.pageSize = this.pageSize;
            this.getDataList(this.dataSearch)
          }
      });
    },    

    getDataList(data) {
      var xdata = data
      xdata.school = this.school
      console.log(xdata,'ss')
      request({
        url: "/workRecord/getWorkRecordList",
        method: "post",
        data: data
      }).then(res => {
        this.dataList = [];
        this.dataList = res.data.workRecord;
        this.dataList.forEach((item, index) => {
          this.dataList[index].staff = item.staff.name
          this.dataList[index].recordType = item.recordType == 0 ? '上班' : '下班'
          this.dataList[index].recordWay = item.recordWay == 0 ? '校内打卡' : '外勤'
          this.dataList[index].createdAt = formatTime.formatTime(item.createdAt)

        });

        this.total = res.data.total;
        this.currentPage = res.data.currentPage;
      });

    },
    formatDate(date) {
      date = new Date(date);
      let year = date.getFullYear();
      let month = date.getMonth() + 1;
      let day = date.getDate();

      return [year, month, day].map(this.formatNumber).join("-");
    },
    formatNumber(n) {
      n = n.toString();
      return n[1] ? n : "0" + n;
    },
  },
  created() {
    this.dataSearch.page = this.currentPage;
    this.dataSearch.pageSize = this.pageSize;
    this.school = this.info.school[0]._id
    this.dataSearch.school = this.school
    this.staff = this.info.staffId
    this.rights = this.info.rights.map(function(e){return e.name})
    let index1 = this.rights.indexOf('root') != -1
    let index2 = this.rights.indexOf('授课老师') != -1
    let index4 = this.rights.indexOf('教务老师') != -1
    index2 = index2 || index4
    let index3 = this.rights.indexOf('行政管理') != -1
            // 
    if ((index1) && (index2)){
        if (index3){
            this.teacher = true
        }
    }
    if (this.$route.query.day != undefined){
      let sss = this.$route.query.day.split('-')
      this.dataSearch.start_time = new Date(sss[0],sss[1]-1,sss[2],0,0,0)
      this.dataSearch.end_time = new Date(sss[0],sss[1]-1,sss[2],23,59,59)
    }
        if (this.$route.query.staff != undefined){
          this.dataSearch.staffId = this.$route.query.staff
    }

    // if (this.staff != null){
    //             this.getInitData(this.dataSearch)
    // }
    // else{
    //           this.$alert('','请先绑定员工')
    // }

  }
};
</script>
